package zone.czh.woi.woim.base.channel.codec;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import zone.czh.woi.protocol.protocol.Payload;
import zone.czh.woi.woim.base.channel.adapter.ProtocolAdapter;
import zone.czh.woi.woim.base.channel.protocol.selector.ProtocolSelector;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*@ClassName: PayloadEncoder
*@Description: None
*@author woi
*/
@ChannelHandler.Sharable
@Getter@Setter
public class PayloadEncoder extends MessageToMessageEncoder<Payload> {

    Map<String, ProtocolAdapter> adapters=new HashMap<>();

    ProtocolSelector protocolSelector;

    @Override
    protected void encode(ChannelHandlerContext ctx, Payload payload, List<Object> list) throws Exception {
        if (adapters!=null){
            if (protocolSelector !=null){
                list.add(adapters.get(protocolSelector.select(ctx.pipeline(),payload)).encode(ctx,payload));
            }else {
                throw new Exception("no protocol selector");
            }
        }else {
            throw new Exception("no protocol adapter");
        }
    }

}
